package com.irun.news.utils;

import com.irun.news.ui.dialog.ProgressDialogLoad;

import android.content.Context;
import android.os.AsyncTask;
import android.text.TextUtils;

/***
 * @author huangsm
 * @date 2013-3-30
 * @email huangsanm@gmail.com
 * @desc 处理http请求
 */
public class RequestTask extends AsyncTask<String, Integer, Object> {
	
	private Context mContext;
	//请求成功
	private boolean isSuc;
	//返回结果
	private Class mCla;
	//回调
	private RequestCallback mCallBack;
	//加载进度
	private ProgressDialogLoad mLoadDialog;
	
	public RequestTask(Context context, Class cla, RequestCallback callback){
		mContext = context;
		mCla = cla;
		mCallBack = callback;
	}
	
	@Override
	protected void onPreExecute() {
		isSuc = false;
		if(mContext != null){
			mLoadDialog = ProgressDialogLoad.show(mContext, null, false);
		}
	}

	@Override
	protected Object doInBackground(String... params) {
		String url = params[0];
		if(TextUtils.isEmpty(url)){
			return Constants.REQUEST_ERROR_URL;
		}
		//解析xml返回结果
		Object obj = null;
		if(obj != null){
			isSuc = true;
			return obj;
		}
		return Constants.REQUEST_ERROR_CONTENT;
	}

	@Override
	protected void onPostExecute(Object result) {
		if(mLoadDialog != null){
			mLoadDialog.cancel();
		}
		
		//返回结果
		if(mCallBack == null){
			mCallBack.callback(isSuc, result);
		}
	}
}
